﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.Models.WorkTask;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SmartSiteManagementSystem.Models.GeneticProgram;
using SmartSiteManagementSystem.Models.DTO;
using SmartSiteManagementSystem.Models.Material;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{
    public class WorkTaskController : AdminBaseControllers
    {

        /// <summary>
        /// 随机数生成器
        /// </summary>
        protected static MyRandom rand = new MyRandom();


        private IWebHostEnvironment _webHostEnvironment;

        private readonly ILogger<WorkTaskController> _logger;
        //注入后台服务
        private readonly IAdminService _adminService;
        private readonly IWorkTaskService _workTaskService;

        private readonly IMaterialService _materialService;

        public WorkTaskController(IAdminService adminService,
            IWorkTaskService workTaskService,
            IMaterialService materialService,
            ILogger<WorkTaskController> logger, IWebHostEnvironment webHostEnvironment) : base(adminService)
        {
            _logger = logger;
            _adminService = adminService;
            _workTaskService = workTaskService;
            _materialService = materialService;
            _webHostEnvironment = webHostEnvironment;
        }

        #region  任务集合

        public IActionResult TaskSetIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            int count = _workTaskService.GetTaskSetInfoListCount(param);
            IList<TaskSetInfo> list = _workTaskService.GetTaskSetInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="setId"></param>
        public JsonResult ajaxDeleteTaskSet(int setId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (setId > 0)
            {
                TaskSetInfo info = _workTaskService.GetTaskSetInfoById(setId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _workTaskService.UpdateTaskSetInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }


        /// <summary>
        /// 添加或修改任务集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditTaskSet(int id = 0)
        {
            TaskSetInfo info = new TaskSetInfo();
            if (id > 0)
            {
                info = _workTaskService.GetTaskSetInfoById(id);
            }
            ViewBag.taskSetInfo = info;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SetId"></param>
        /// <param name="SetName"></param>
        /// <param name="Descript"></param>
        public JsonResult ajaxAddOrEditTaskSet(int SetId, string SetName,string Descript)
        {
            TaskSetInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";

            bool success = false;
            int code = -1;
            if (SetId > 0)
            {
                info = _workTaskService.GetTaskSetInfoById(SetId);
            }
            else
            {
                info = new TaskSetInfo();
            }
            info.IsDelete = false;
            info.SetName = SetName;
            info.Descript = Descript;
            if (SetId > 0)
            {
                success = _workTaskService.UpdateTaskSetInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                SetId = _workTaskService.InsertTaskSetInfo(info);
                success = SetId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }

        #endregion

        #region 供应地
        public IActionResult SupplyingPlaceIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetSupplyingPlaceList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            int count = _workTaskService.GetSupplyingPlaceInfoListCount(param);
            IList<SupplyingPlaceInfo> list = _workTaskService.GetSupplyingPlaceInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="supplyingPlaceId"></param>
        public JsonResult ajaxDeleteSupplyingPlace(int supplyingPlaceId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (supplyingPlaceId > 0)
            {
                SupplyingPlaceInfo info = _workTaskService.GetSupplyingPlaceInfoById(supplyingPlaceId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _workTaskService.UpdateSupplyingPlaceInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }



        /// <summary>
        /// 添加或修改供应地
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditSupplyingPlace(int id = 0)
        {
            SupplyingPlaceInfo info = new SupplyingPlaceInfo();
            if (id > 0)
            {
                info = _workTaskService.GetSupplyingPlaceInfoById(id);
            }
            ViewBag.supplyingPlaceInfo = info;

            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="SupplyingPlaceId"></param>
        /// <param name="SupplyingPlaceName"></param>
        /// <param name="PositionX"></param>
        /// <param name="PositionY"></param>
        /// <param name="PositionZ"></param>
        /// <param name="Descript"></param>
        public JsonResult ajaxAddOrEditSupplyingPlace(int SupplyingPlaceId,
            double PositionX, double PositionY, double PositionZ,
            string SupplyingPlaceName, string Descript)
        {
            SupplyingPlaceInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            bool success = false;
            int code = -1;
            if (SupplyingPlaceId > 0)
            {
                info = _workTaskService.GetSupplyingPlaceInfoById(SupplyingPlaceId);
            }
            else
            {
                info = new SupplyingPlaceInfo();
            }
            info.IsDelete = false;
            info.PositionX = PositionX;
            info.PositionY = PositionY;
            info.PositionZ = PositionZ;
            info.SupplyingPlaceName = SupplyingPlaceName;

            info.Descript = Descript;
            if (SupplyingPlaceId > 0)
            {
                success = _workTaskService.UpdateSupplyingPlaceInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                SupplyingPlaceId = _workTaskService.InsertSupplyingPlaceInfo(info);
                success = SupplyingPlaceId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }

        #endregion



        #region 需求地
        public IActionResult DemandingPlaceIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetDemandingPlaceList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_deleted", 0);//正常 0 false
            int count = _workTaskService.GetDemandingPlaceInfoListCount(param);
            IList<DemandingPlaceInfo> list = _workTaskService.GetDemandingPlaceInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };

            return Json(data);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="demandingPlaceId"></param>
        public JsonResult ajaxDeleteDemandingPlace(int demandingPlaceId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (demandingPlaceId > 0)
            {
                DemandingPlaceInfo info = _workTaskService.GetDemandingPlaceInfoById(demandingPlaceId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _workTaskService.UpdateDemandingPlaceInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }



        /// <summary>
        /// 添加或修改需求地
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditDemandingPlace(int id = 0)
        {
            DemandingPlaceInfo info = new DemandingPlaceInfo();
            if (id > 0)
            {
                info = _workTaskService.GetDemandingPlaceInfoById(id);
            }
            ViewBag.demandingPlaceInfo = info;

            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="DemandingPlaceId"></param>
        /// <param name="DemandingPlaceName"></param>
        /// <param name="PositionX"></param>
        /// <param name="PositionY"></param>
        /// <param name="PositionZ"></param>
        /// <param name="Descript"></param>
        public JsonResult ajaxAddOrEditDemandingPlace(int DemandingPlaceId,
            double PositionX, double PositionY, double PositionZ,
            string DemandingPlaceName, string Descript)
        {
            DemandingPlaceInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            bool success = false;
            int code = -1;
            if (DemandingPlaceId > 0)
            {
                info = _workTaskService.GetDemandingPlaceInfoById(DemandingPlaceId);
            }
            else
            {
                info = new DemandingPlaceInfo();
            }
            info.IsDelete = false;
            info.PositionX = PositionX;
            info.PositionY = PositionY;
            info.PositionZ = PositionZ;
            info.DemandingPlaceName = DemandingPlaceName;

            info.Descript = Descript;
            if (DemandingPlaceId > 0)
            {
                success = _workTaskService.UpdateDemandingPlaceInfo(info);
                if (success)
                {
                    code = 1;
                }
            }
            else
            {
                DemandingPlaceId = _workTaskService.InsertDemandingPlaceInfo(info);
                success = DemandingPlaceId > 0 ? true : false;
                if (success)
                {
                    code = 1;
                }
            }
            if (code == 1)
            {
                result.IsSuccess = true;
                result.Msg = "操作成功!";
            }
            result.Code = code;
            return Json(result);
        }

        #endregion

        #region 任务 taskInfo
        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskSetId"></param>
        /// <returns></returns>
        public IActionResult taskIndex(int taskSetId)
        {
            ViewBag.taskSetId = taskSetId;
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetTaskList(int taskSetId,string searchKey, int page = 0, int rows = 50)
        {


            TaskSetInfo info = _workTaskService.GetTaskSetInfoById(taskSetId);


            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("taskset_id", taskSetId);//正常 0 false
            param.Add("is_deleted", 0);//正常 0 false
            int count = _workTaskService.GetTaskInfoListCount(param);
            IList<TaskInfo> list = _workTaskService.GetTaskInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };

            return Json(data);

        }



        /// <summary>
        /// 批量生成任务
        /// </summary>
        /// <param name="taskSetId"></param>
        /// <param name="generalTaskCount"></param>
        public JsonResult ajaxGeneralTask(int taskSetId, int generalTaskCount)
        {

            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            IList<TaskInfo> taskinfoList = new List<TaskInfo>();
            if (generalTaskCount > 0)
            {
                TaskSetInfo taskSetInfo = _workTaskService.GetTaskSetInfoById(taskSetId);

                Hashtable param = new Hashtable();
                param.Add("is_delete", 0);//正常 0 false
                IList<DemandingPlaceInfo> demandingPlaceInfoList = _workTaskService.GetDemandingPlaceInfoList(param, "", 0, 500);

                Hashtable mparam = new Hashtable();
                mparam.Add("is_deleted", 0);//正常 0 false
                IList<MaterialInfo> materialInfoList = _materialService.GetMaterialInfoList(mparam, "", 0, 500);

                int demandMaxCount = demandingPlaceInfoList.Count;
                int materialMaxCount = materialInfoList.Count;
                double lastTaskAt = 0f; //上一步到达时间
                for (int i = 0; i < generalTaskCount; i++)
                {
                    TaskInfo addTask = new TaskInfo();
                    addTask.TaskName = "任务" + (i + 1).ToString();
                    addTask.TaskSetId = taskSetId;
                    addTask.Descript = "任务" + (i + 1).ToString();
                    addTask.CreateTime = DateTime.Now;
                    addTask.UpdateTime = DateTime.Now;
                    addTask.IsDelete = false;
                    addTask.BatchId = DateTime.Now.Year * 10000 + DateTime.Now.Month * 100 + DateTime.Now.Day;
                    #region 随机需求地
                    int demandVal = rand.Next(demandMaxCount); //随机生成数据的下标
                    DemandingPlaceInfo demandPositionPlaceInfo = demandingPlaceInfoList[demandVal];
                    addTask.DemandingPlacePositionX = demandPositionPlaceInfo.PositionX;
                    addTask.DemandingPlacePositionY = demandPositionPlaceInfo.PositionY;
                    addTask.DemandingPlacePositionZ = demandPositionPlaceInfo.PositionZ;
                    addTask.DemandingPlaceId = demandPositionPlaceInfo.DemandingPlaceId;

                    #endregion
                    #region 随机权重
                    int weightRandVal = rand.Next(3);
                    int weightValue = 1;
                    switch (weightRandVal)
                    {
                        case 0:
                            {
                                weightValue = 1;
                                break;
                            }
                        case 1:
                            {
                                weightValue = 2;
                                break;
                            }
                        case 2:
                            {
                                weightValue = 4;
                                break;
                            }
                    }
                    #endregion
                    addTask.Weight = weightValue;
                    #region 随机宽裕度系数
                    int slatRandVal = rand.Next(4);
                    int slaValue = 4;
                    switch (slatRandVal)
                    {
                        case 0:
                            {
                                slaValue = 4;
                                break;
                            }
                        case 1:
                            {
                                slaValue = 8;
                                break;
                            }
                        case 2:
                            {
                                slaValue = 12;
                                break;
                            }
                        case 3:
                            {
                                slaValue = 999;
                                break;
                            }
                    }

                    addTask.SLA = slaValue;
                    #endregion

                    #region 随机任务需要物料的类型 20220406 add

                    int materialVal = rand.Next(materialMaxCount); //随机生成数据的下标
                    MaterialInfo materialInfo = materialInfoList[materialVal];
                    addTask.DemandMaterialId = materialInfo.MaterialId;
                    #endregion

                    #region 前5个任务到达时间为0
                    double at = 0;
                    if (i >= 5)
                    {

                        at = lastTaskAt + rand.Next(0, 50);

                    }
                    #endregion
                    addTask.MaterialArrivalTime = DateTime.Now.AddMinutes(at);
                    addTask.ArrivalTime = at;

                    taskinfoList.Add(addTask);
                    _workTaskService.InsertTaskInfo(addTask);
                    lastTaskAt = addTask.ArrivalTime;
                }
            }

            result.IsSuccess = true;
            result.Msg = "操作成功!";
            return Json(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="taskId"></param>
        public JsonResult ajaxDeleteTask(int taskId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (taskId > 0)
            {
                TaskInfo info = _workTaskService.GetTaskInfoById(taskId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess =       _workTaskService.UpdateTaskInfo(info);
                }
            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="taskSetId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditTask(int taskSetId,int id = 0)
        {
            TaskInfo info = new TaskInfo();
            if (id > 0)
            {
                info = _workTaskService.GetTaskInfoById(id);
            }
            ViewBag.taskInfo = info;

            return View();
        }


        #endregion


    }
}
